home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / tools / dfue / term 4.6(?) / extras / source / term-source.lha / Print.c < prev    next >
C/C++ Source or Header  |  1996-03-18  |  24KB  |  1,141 lines

  1. /*
  2. **    Print.c
  3. **
  4. **    Printer control routines
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "Global.h"
  14. #endif
  15.  
  16.     /* PrintText(BPTR File,struct Window *ReqWindow,LONG *Error,STRPTR String,...):
  17.      *
  18.      *    Output a printf() style message.
  19.      */
  20.  
  21. BOOL __stdargs
  22. PrintText(BPTR File,struct Window *ReqWindow,LONG *Error,STRPTR String,...)
  23. {
  24.     va_list    VarArgs;
  25.     LONG    Len;
  26.  
  27.     va_start(VarArgs,String);
  28.     VSPrintf(SharedBuffer,String,VarArgs);
  29.     va_end(VarArgs);
  30.  
  31.     if(ReqWindow)
  32.     {
  33.         if(!SysReqHandler(ReqWindow,NULL,FALSE))
  34.         {
  35.             *Error = 0;
  36.  
  37.             return(FALSE);
  38.         }
  39.     }
  40.     else
  41.     {
  42.         if(CheckSignal(SIG_BREAK))
  43.         {
  44.             *Error = 0;
  45.  
  46.             return(FALSE);
  47.         }
  48.     }
  49.  
  50.     Len = strlen(SharedBuffer) + 1;
  51.  
  52.     SetIoErr(0);
  53.  
  54.     strcat(SharedBuffer,"\n");
  55.  
  56.     if(Write(File,SharedBuffer,Len) < Len)
  57.     {
  58.         *Error = IoErr();
  59.  
  60.         return(FALSE);
  61.     }
  62.     else
  63.         return(TRUE);
  64. }
  65.  
  66.     /* PrintHeader(BPTR File,struct Window *ReqWindow,LONG *Error,ULONG Code):
  67.      *
  68.      *    Print a line header.
  69.      */
  70.  
  71. STATIC BOOL
  72. PrintHeader(BPTR File,struct Window *ReqWindow,LONG *Error,ULONG Code,BOOL Plain)
  73. {
  74.     STRPTR    String;
  75.     LONG    Len;
  76.  
  77.     String = LocaleString(Code);
  78.  
  79.     if(ReqWindow)
  80.     {
  81.         if(!SysReqHandler(ReqWindow,NULL,FALSE))
  82.         {
  83.             *Error = 0;
  84.  
  85.             return(FALSE);
  86.         }
  87.     }
  88.     else
  89.     {
  90.         if(CheckSignal(SIG_BREAK))
  91.         {
  92.             *Error = 0;
  93.  
  94.             return(FALSE);
  95.         }
  96.     }
  97.  
  98.     if(!Plain)
  99.         SPrintf(String = SharedBuffer,"\33[1m%s\33[0m",String);
  100.  
  101.     SetIoErr(0);
  102.  
  103.     Len = strlen(String);
  104.  
  105.     if(Write(File,String,Len) < Len)
  106.     {
  107.         *Error = IoErr();
  108.  
  109.         return(FALSE);
  110.     }
  111.  
  112.     return(TRUE);
  113. }
  114.  
  115.     /* PrintFileInformation():
  116.      *
  117.      *    Print information on a file.
  118.      */
  119.  
  120. BOOL
  121. PrintFileInformation(BPTR File,struct Window *ReqWindow,LONG *Error,STRPTR Name,ULONG Flags)
  122. {
  123.     BOOL Continue;
  124.  
  125.         /* Any special information to print along with the name? */
  126.  
  127.     if(Flags)
  128.     {
  129.         BPTR FileLock;
  130.  
  131.             /* Try to grip the file. */
  132.  
  133.         if(FileLock = Lock(Name,ACCESS_READ))
  134.         {
  135.             struct FileInfoBlock *FileInfo;
  136.  
  137.                 /* Allocate info buffer. */
  138.  
  139.             if(FileInfo = (struct FileInfoBlock *)AllocDosObject(DOS_FIB,TAG_DONE))
  140.             {
  141.                     /* How does it look like? */
  142.  
  143.                 if(Examine(FileLock,FileInfo))
  144.                 {
  145.                     UBYTE    DummyBuffer[300];
  146.                     STRPTR    Index;
  147.  
  148.                         /* Add the size. */
  149.  
  150.                     if(Flags & PRINT_SIZE)
  151.                         SPrintf(DummyBuffer,"%-25s %7ld",FilePart(Name),FileInfo -> fib_Size);
  152.                     else
  153.                         SPrintf(DummyBuffer,"%-25s",FilePart(Name));
  154.  
  155.                     Index = DummyBuffer;
  156.  
  157.                         /* Find the end of the string. */
  158.  
  159.                     while(*Index)
  160.                         Index++;
  161.  
  162.                         /* Add the protection bits. */
  163.  
  164.                     if(Flags & PRINT_BITS)
  165.                     {
  166.                         STATIC STRPTR    SetBits = "----aps",
  167.                                         ClrBits = "dewr---";
  168.  
  169.                         UBYTE TempString[10];
  170.  
  171.                         LONG i;
  172.  
  173.                         strcpy(TempString," -------");
  174.  
  175.                         for(i = 0 ; i < 7 ; i++)
  176.                         {
  177.                             if(FileInfo -> fib_Protection & (1L << i))
  178.                                 TempString[6 - i + 1] = SetBits[i];
  179.                             else
  180.                                 TempString[6 - i + 1] = ClrBits[i];
  181.                         }
  182.  
  183.                         strcpy(Index,TempString);
  184.  
  185.                         while(*Index)
  186.                             Index++;
  187.                     }
  188.  
  189.                         /* Add the creation date. */
  190.  
  191.                     if(Flags & PRINT_DATE)
  192.                     {
  193.                         UBYTE            Date[20],
  194.                                         Time[20];
  195.                         struct DateTime    DateTime;
  196.  
  197.                             /* Prepare for date conversion. */
  198.  
  199.                         DateTime . dat_Stamp    = FileInfo -> fib_Date;
  200.                         DateTime . dat_Format    = FORMAT_DOS;
  201.                         DateTime . dat_Flags    = DTF_SUBST;
  202.                         DateTime . dat_StrDay    = NULL;
  203.                         DateTime . dat_StrDate    = Date;
  204.                         DateTime . dat_StrTime    = Time;
  205.  
  206.                             /* Convert the date. */
  207.  
  208.                         if(DateToStr(&DateTime))
  209.                         {
  210.                             SPrintf(Index," %-9s %s",Date,Time);
  211.  
  212.                             while(*Index)
  213.                                 Index++;
  214.                         }
  215.                     }
  216.  
  217.                         /* Add the file comment. */
  218.  
  219.                     if(Flags & PRINT_COMMENT)
  220.                         SPrintf(Index,"\n: %s",FileInfo -> fib_Comment);
  221.  
  222.                     Continue = PrintText(File,ReqWindow,Error,"%s\n",DummyBuffer);
  223.                 }
  224.                 else
  225.                     Continue = FALSE;
  226.  
  227.                 FreeDosObject(DOS_FIB,FileInfo);
  228.             }
  229.             else
  230.                 Continue = FALSE;
  231.  
  232.             UnLock(FileLock);
  233.         }
  234.         else
  235.             Continue = FALSE;
  236.     }
  237.     else
  238.         Continue = PrintText(File,ReqWindow,Error,"%s\n",Name);
  239.  
  240.     return(Continue);
  241. }
  242.  
  243.     /* PrintEntry(BPTR File,struct Window *ReqWindow,LONG *Error,struct PhoneEntry *Entry):
  244.      *
  245.      *    Print information on the contents of a phonebook entry.
  246.      */
  247.  
  248. BOOL
  249. PrintEntry(BPTR File,struct Window *ReqWindow,BOOL Plain,LONG *Error,struct PhoneEntry *Entry,ULONG Flags)
  250. {
  251.     if(Plain)
  252.     {
  253.         if(!PrintText(File,ReqWindow,Error,"\n\"%s\" (%s)",Entry -> Header -> Name,Entry -> Header -> Number))
  254.             return(FALSE);
  255.     }
  256.     else
  257.     {
  258.         if(!PrintText(File,ReqWindow,Error,"\n\33[4m\"%s\" (%s)\33[0m",Entry -> Header -> Name,Entry -> Header -> Number))
  259.             return(FALSE);
  260.     }
  261.  
  262.     if(Flags & PRINT_COMMENT)
  263.     {
  264.         if(Entry -> Header -> Comment[0])
  265.         {
  266.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_COMMENT_TXT,Plain))
  267.                 return(FALSE);
  268.  
  269.             if(!PrintText(File,ReqWindow,Error,Entry -> Header -> Comment))
  270.                 return(FALSE);
  271.         }
  272.     }
  273.  
  274.     if(Flags & PRINT_USERNAME)
  275.     {
  276.         if(Entry -> Header -> UserName[0])
  277.         {
  278.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_USER_NAME_TXT,Plain))
  279.                 return(FALSE);
  280.  
  281.             if(!PrintText(File,ReqWindow,Error,Entry -> Header -> UserName))
  282.                 return(FALSE);
  283.         }
  284.     }
  285.  
  286.     if((Flags & PRINT_SERIAL) && Entry -> Config -> SerialConfig)
  287.     {
  288.         STATIC UBYTE Parities[] =
  289.         {
  290.             'N','E','O','M','S'
  291.         };
  292.  
  293.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_BAUD_RATE_TXT,Plain))
  294.             return(FALSE);
  295.  
  296.         if(LocaleBase)
  297.         {
  298.             if(!PrintText(File,ReqWindow,Error,"%lD",Entry -> Config -> SerialConfig -> BaudRate))
  299.                 return(FALSE);
  300.         }
  301.         else
  302.         {
  303.             if(!PrintText(File,ReqWindow,Error,"%ld",Entry -> Config -> SerialConfig -> BaudRate))
  304.                 return(FALSE);
  305.         }
  306.  
  307.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_PARAMETERS_TXT,Plain))
  308.             return(FALSE);
  309.  
  310.         if(!PrintText(File,ReqWindow,Error,"%ld-%lc-%ld",Entry -> Config -> SerialConfig -> BitsPerChar,Parities[Entry -> Config -> SerialConfig -> Parity],Entry -> Config -> SerialConfig -> StopBits))
  311.             return(FALSE);
  312.  
  313.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_HANDSHAKING_TXT,Plain))
  314.             return(FALSE);
  315.  
  316.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_SERIALPANEL_HANDSHAKING_NONE_TXT + Entry -> Config -> SerialConfig -> HandshakingProtocol)))
  317.             return(FALSE);
  318.  
  319.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DUPLEX_TXT,Plain))
  320.             return(FALSE);
  321.  
  322.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_SERIALPANEL_DUPLEX_FULL_TXT + Entry -> Config -> SerialConfig -> Duplex)))
  323.             return(FALSE);
  324.  
  325.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_STRIP_BIT_TXT,Plain))
  326.             return(FALSE);
  327.  
  328.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_DISABLED_TXT + Entry -> Config -> SerialConfig -> StripBit8)))
  329.             return(FALSE);
  330.  
  331.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_FLOW_CONTROL_TXT,Plain))
  332.             return(FALSE);
  333.  
  334.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_DISABLED_TXT + Entry -> Config -> SerialConfig -> xONxOFF)))
  335.             return(FALSE);
  336.  
  337.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_SERIAL_DRIVER_TXT,Plain))
  338.             return(FALSE);
  339.  
  340.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_NAME_UNIT_TEMPLATE_TXT),Entry -> Config -> SerialConfig -> SerialDevice, + Entry -> Config -> SerialConfig -> UnitNumber))
  341.             return(FALSE);
  342.     }
  343.  
  344.     if((Flags & PRINT_MODEM) && Entry -> Config -> ModemConfig)
  345.     {
  346.         if(Entry -> Config -> ModemConfig -> ModemInit[0])
  347.         {
  348.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_MODEM_INIT_TXT,Plain))
  349.                 return(FALSE);
  350.  
  351.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry -> Config -> ModemConfig -> ModemInit))
  352.                 return(FALSE);
  353.         }
  354.  
  355.         if(Entry -> Config -> ModemConfig -> ModemExit[0])
  356.         {
  357.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_MODEM_EXIT_TXT,Plain))
  358.                 return(FALSE);
  359.  
  360.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry -> Config -> ModemConfig -> ModemExit))
  361.                 return(FALSE);
  362.         }
  363.  
  364.         if(Entry -> Config -> ModemConfig -> ModemHangup[0])
  365.         {
  366.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_HANG_UP_TXT,Plain))
  367.                 return(FALSE);
  368.  
  369.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry -> Config -> ModemConfig -> ModemHangup))
  370.                 return(FALSE);
  371.         }
  372.  
  373.         if(Entry -> Config -> ModemConfig -> DialPrefix[0])
  374.         {
  375.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DIAL_PREFIX_TXT,Plain))
  376.                 return(FALSE);
  377.  
  378.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry -> Config -> ModemConfig -> DialPrefix))
  379.                 return(FALSE);
  380.         }
  381.  
  382.         if(Entry -> Config -> ModemConfig -> DialSuffix[0])
  383.         {
  384.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DIAL_SUFFIX_TXT,Plain))
  385.                 return(FALSE);
  386.  
  387.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry -> Config -> ModemConfig -> DialSuffix))
  388.                 return(FALSE);
  389.         }
  390.  
  391.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_REDIAL_DELAY_TXT,Plain))
  392.             return(FALSE);
  393.  
  394.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_MINUTE_SECOND_TEMPLATE_TXT),Entry -> Config -> ModemConfig -> RedialDelay / 60,Entry -> Config -> ModemConfig -> RedialDelay % 60))
  395.             return(FALSE);
  396.  
  397.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DIAL_TIMEOUT_TXT,Plain))
  398.             return(FALSE);
  399.  
  400.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_MINUTE_SECOND_TEMPLATE_TXT),Entry -> Config -> ModemConfig -> DialTimeout / 60,Entry -> Config -> ModemConfig -> DialTimeout % 60))
  401.             return(FALSE);
  402.  
  403.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_AUTO_BAUD_TXT,Plain))
  404.             return(FALSE);
  405.  
  406.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_DISABLED_TXT + Entry -> Config -> ModemConfig -> ConnectAutoBaud)))
  407.             return(FALSE);
  408.  
  409.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DROP_DTR_TXT,Plain))
  410.             return(FALSE);
  411.  
  412.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_DISABLED_TXT + Entry -> Config -> ModemConfig -> DropDTR)))
  413.             return(FALSE);
  414.     }
  415.  
  416.     if((Flags & PRINT_SCREEN) && Entry -> Config -> ScreenConfig)
  417.     {
  418.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_DISPLAY_MODE_TXT,Plain))
  419.             return(FALSE);
  420.  
  421.         if(!PrintText(File,ReqWindow,Error,GetModeName(Entry -> Config -> ScreenConfig -> DisplayMode)))
  422.             return(FALSE);
  423.  
  424.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_COLOUR_MODE_TXT,Plain))
  425.             return(FALSE);
  426.  
  427.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_SCREENPANEL_COLOUR_AMIGA_TXT + Entry -> Config -> ScreenConfig -> ColourMode)))
  428.             return(FALSE);
  429.     }
  430.  
  431.     if((Flags & PRINT_TERMINAL) && Entry -> Config -> TerminalConfig)
  432.     {
  433.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_TERMINAL_EMULATION_TXT,Plain))
  434.             return(FALSE);
  435.  
  436.         if(Entry -> Config -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL)
  437.         {
  438.             if(!PrintText(File,ReqWindow,Error,"%s, \"%s\"",LocaleString(MSG_TERMINALPANEL_EMULATION_ANSI_VT102_TXT + Entry -> Config -> TerminalConfig -> EmulationMode),Entry -> Config -> TerminalConfig -> EmulationMode))
  439.                 return(FALSE);
  440.         }
  441.         else
  442.         {
  443.             if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_TERMINALPANEL_EMULATION_ANSI_VT102_TXT + Entry -> Config -> TerminalConfig -> EmulationMode)))
  444.                 return(FALSE);
  445.         }
  446.  
  447.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_FONT_TXT,Plain))
  448.             return(FALSE);
  449.  
  450.         if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_TERMINALPANEL_FONT_STANDARD_TXT + Entry -> Config -> TerminalConfig -> FontMode)))
  451.             return(FALSE);
  452.  
  453.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_TEXT_COLUMNS_TXT,Plain))
  454.             return(FALSE);
  455.  
  456.         if(Entry -> Config -> TerminalConfig -> NumColumns < 20)
  457.         {
  458.             if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_MAXIMUM_TXT)))
  459.                 return(FALSE);
  460.         }
  461.         else
  462.         {
  463.             if(!PrintText(File,ReqWindow,Error,"%ld",Entry -> Config -> TerminalConfig -> NumColumns))
  464.                 return(FALSE);
  465.         }
  466.  
  467.         if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_TEXT_LINES_TXT,Plain))
  468.             return(FALSE);
  469.  
  470.         if(Entry -> Config -> TerminalConfig -> NumLines < 20)
  471.         {
  472.             if(!PrintText(File,ReqWindow,Error,LocaleString(MSG_PRINTPANEL_MAXIMUM_TXT)))
  473.                 return(FALSE);
  474.         }
  475.         else
  476.         {
  477.             if(!PrintText(File,ReqWindow,Error,"%ld",Entry -> Config -> TerminalConfig -> NumLines))
  478.                 return(FALSE);
  479.         }
  480.  
  481.         if(Entry -> Config -> TerminalConfig -> KeyMapFileName[0])
  482.         {
  483.             if(!PrintHeader(File,ReqWindow,Error,MSG_PRINTPANEL_KEYMAP_FILE_TXT,Plain))
  484.                 return(FALSE);
  485.  
  486.             if(!PrintText(File,ReqWindow,Error,"\"%s\"",Entry -> Config -> TerminalConfig -> KeyMapFileName))
  487.                 return(FALSE);
  488.         }
  489.     }
  490.  
  491.     return(TRUE);
  492. }
  493.  
  494.     /* PrintScreen(BPTR File,struct Window *ReqWindow,LONG *Error):
  495.      *
  496.      *    Print the contents of the screen, requires the raster
  497.      *    to be available.
  498.      */
  499.  
  500. BOOL
  501. PrintScreen(BPTR File,struct Window *ReqWindow,LONG *Error)
  502. {
  503.     LONG     i,j;
  504.     UBYTE    *Buffer;
  505.  
  506.         /* Run down the lines... */
  507.  
  508.     for(i = 0 ; i <= LastLine ; i++)
  509.     {
  510.             /* Grab the line. */
  511.  
  512.         Buffer = &Raster[i * RasterWidth];
  513.  
  514.         j = LastColumn;
  515.  
  516.             /* Strip trailing spaces. */
  517.  
  518.         while(j >= 0 && Buffer[j] == ' ')
  519.             j--;
  520.  
  521.             /* Blank line? */
  522.  
  523.         if(j >= 0)
  524.         {
  525.             SetIoErr(0);
  526.  
  527.             if(Write(File,Buffer,j + 1) < j + 1)
  528.             {
  529.                 *Error = IoErr();
  530.  
  531.                 return(FALSE);
  532.             }
  533.         }
  534.  
  535.             /* Is printing to be aborted? */
  536.  
  537.         if(!SysReqHandler(ReqWindow,NULL,FALSE))
  538.         {
  539.             *Error = 0;
  540.  
  541.             return(FALSE);
  542.         }
  543.  
  544.             /* Add line terminator. */
  545.  
  546.         SetIoErr(0);
  547.  
  548.         if(Write(File,"\n",1) < 1)
  549.         {
  550.             *Error = IoErr();
  551.  
  552.             return(FALSE);
  553.         }
  554.  
  555.             /* Is printing to be aborted? */
  556.  
  557.         if(!SysReqHandler(ReqWindow,NULL,FALSE))
  558.         {
  559.             *Error = 0;
  560.  
  561.             return(FALSE);
  562.         }
  563.     }
  564.  
  565.     return(TRUE);
  566. }
  567.  
  568.     /* PrintClip(BPTR File,struct Window *ReqWindow,LONG *Error):
  569.      *
  570.      *    Print the contents of the clipboard.
  571.      */
  572.  
  573. BOOL
  574. PrintClip(BPTR File,struct Window *ReqWindow,LONG *Error)
  575. {
  576.     LONG ClipError;
  577.  
  578.         /* Are we currently reading input from the
  579.          * clipboard? If so, close it.
  580.          */
  581.  
  582.     if(ClipInput)
  583.     {
  584.         CloseClip();
  585.  
  586.         ClipInput = ClipXerox = FALSE;
  587.     }
  588.  
  589.         /* Open the clipboard for reading. */
  590.  
  591.     if(ClipError = OpenClip(Config -> ClipConfig -> ClipboardUnit))
  592.     {
  593.         *Error = ERROR_OBJECT_NOT_FOUND;
  594.  
  595.         return(FALSE);
  596.     }
  597.     else
  598.     {
  599.         UBYTE    InputBuffer[257];
  600.         LONG    Len;
  601.  
  602.             /* Read clipboard contents. */
  603.  
  604.         while((Len = GetClip(InputBuffer,256,TRUE)) > 0)
  605.         {
  606.                 /* Are we to stop printing? */
  607.  
  608.             if(!SysReqHandler(ReqWindow,NULL,FALSE))
  609.             {
  610.                 *Error = 0;
  611.  
  612.                 CloseClip();
  613.  
  614.                 return(FALSE);
  615.             }
  616.             else
  617.             {
  618.                 SetIoErr(0);
  619.  
  620.                 if(Write(File,InputBuffer,Len) < Len)
  621.                 {
  622.                     *Error = IoErr();
  623.  
  624.                     CloseClip();
  625.  
  626.                     return(FALSE);
  627.                 }
  628.             }
  629.         }
  630.  
  631.         if(Len < 0)
  632.         {
  633.             if(SysReqHandler(ReqWindow,NULL,FALSE) == -2)
  634.             {
  635.                 SetIoErr(0);
  636.  
  637.                 if(Write(File,"\n",1) < 1)
  638.                     *Error = IoErr();
  639.  
  640.                 CloseClip();
  641.  
  642.                 return(FALSE);
  643.             }
  644.         }
  645.  
  646.         CloseClip();
  647.     }
  648.  
  649.     return(TRUE);
  650. }
  651.  
  652.     /* PrintBuffer(BPTR File,struct Window *ReqWindow,LONG *Error):
  653.      *
  654.      *    Print the contents of the text buffer.
  655.      */
  656.  
  657. BOOL
  658. PrintBuffer(BPTR File,struct Window *ReqWindow,LONG *Error)
  659. {
  660.     BOOL Continue = TRUE;
  661.     LONG i,Len;
  662.  
  663.     ObtainSemaphore(BufferSemaphore);
  664.  
  665.     if(BufferLines)
  666.     {
  667.         for(i = 0 ; i < Lines ; i++)
  668.         {
  669.             Len = BufferLines[i][-1];
  670.  
  671.             if(!SysReqHandler(ReqWindow,NULL,FALSE))
  672.             {
  673.                 *Error = 0;
  674.  
  675.                 Continue = FALSE;
  676.  
  677.                 break;
  678.             }
  679.  
  680.             if(Len)
  681.             {
  682.                 SetIoErr(0);
  683.  
  684.                 if(Write(File,BufferLines[i],Len) < Len)
  685.                 {
  686.                     *Error = IoErr();
  687.  
  688.                     Continue = FALSE;
  689.  
  690.                     break;
  691.                 }
  692.             }
  693.  
  694.             if(!SysReqHandler(ReqWindow,NULL,FALSE))
  695.             {
  696.                 *Error = 0;
  697.  
  698.                 Continue = FALSE;
  699.  
  700.                 break;
  701.             }
  702.  
  703.             SetIoErr(0);
  704.  
  705.             if(Write(File,"\n",1) < 1)
  706.             {
  707.                 *Error = IoErr();
  708.  
  709.                 Continue = FALSE;
  710.  
  711.                 break;
  712.             }
  713.         }
  714.     }
  715.     else
  716.         Continue = FALSE;
  717.  
  718.     ReleaseSemaphore(BufferSemaphore);
  719.  
  720.     return(Continue);
  721. }
  722.  
  723.     /* PrintSomething(BYTE Source):
  724.      *
  725.      *    Print the screen or the current contents of the clipboard.
  726.      */
  727.  
  728. VOID
  729. PrintSomething(LONG Source)
  730. {
  731.     struct Window        *ReqWindow;
  732.     struct EasyStruct     Easy;
  733.     LONG                 Error = 0;
  734.  
  735.         /* Fill in the easy requester structure. */
  736.  
  737.     Easy . es_StructSize    = sizeof(struct EasyStruct);
  738.     Easy . es_Flags            = NULL;
  739.     Easy . es_Title            = (STRPTR)LocaleString(MSG_TERMAUX_TERM_REQUEST_TXT);
  740.     Easy . es_GadgetFormat    = (STRPTR)LocaleString(MSG_PRINT_STOP_TXT);
  741.  
  742.     if(Source == PRINT_CLIP)
  743.         Easy . es_TextFormat = (STRPTR)LocaleString(MSG_PRINT_PRINTING_CLIP_TXT);
  744.     else
  745.         Easy . es_TextFormat = (STRPTR)LocaleString(MSG_PRINT_PRINTING_SCREEN_TXT);
  746.  
  747.         /* The requester is to be displayed while printing. */
  748.  
  749.     if(ReqWindow = BuildEasyRequest(Window,&Easy,NULL))
  750.     {
  751.         BPTR SomeFile;
  752.  
  753.             /* Add header information if printer channel is already open. */
  754.  
  755.         if(PrinterCapture)
  756.         {
  757.             LONG Len = strlen(LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_FOLLOWS_TXT));
  758.  
  759.             if(Write(PrinterCapture,LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_FOLLOWS_TXT),Len) < Len)
  760.             {
  761.                 FreeSysRequest(ReqWindow);
  762.  
  763.                 ReqWindow = NULL;
  764.  
  765.                 Error = IoErr();
  766.             }
  767.             else
  768.                 SomeFile = PrinterCapture;
  769.         }
  770.         else
  771.         {
  772.                 /* Open printer channel. */
  773.  
  774.             if(!(SomeFile = Open("PRT:",MODE_NEWFILE)))
  775.             {
  776.                 FreeSysRequest(ReqWindow);
  777.  
  778.                 ReqWindow = NULL;
  779.  
  780.                 Error = IoErr();
  781.             }
  782.         }
  783.  
  784.             /* Everything fine so far? */
  785.  
  786.         if(!Error)
  787.         {
  788.             BOOL Stopped;
  789.  
  790.                 /* Are we to print the screen? */
  791.  
  792.             if(Source == PRINT_SCREEN)
  793.                 Stopped = !PrintScreen(SomeFile,ReqWindow,&Error);
  794.             else
  795.                 Stopped = !PrintClip(SomeFile,ReqWindow,&Error);
  796.  
  797.                 /* Add a trailer if necessary. */
  798.  
  799.             if(PrinterCapture)
  800.             {
  801.                 if(!Error && !Stopped)
  802.                 {
  803.                     LONG Len;
  804.  
  805.                     SetIoErr(0);
  806.  
  807.                     Len = strlen(LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_ENDING_TXT));
  808.  
  809.                     if(Write(PrinterCapture,LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_ENDING_TXT),Len) < Len)
  810.                         Error = IoErr();
  811.                 }
  812.             }
  813.             else
  814.             {
  815.                     /* Close the printer stream. */
  816.  
  817.                 if(!Close(SomeFile))
  818.                     Error = IoErr();
  819.             }
  820.         }
  821.  
  822.             /* Release the system requster. */
  823.  
  824.         if(ReqWindow)
  825.             FreeSysRequest(ReqWindow);
  826.  
  827.             /* Display the error code if necessary. */
  828.  
  829.         if(Error)
  830.         {
  831.             STRPTR ErrorString;
  832.  
  833.             if(Fault(Error,"",SharedBuffer,256))
  834.                 ErrorString = SharedBuffer;
  835.             else
  836.                 ErrorString = "???";
  837.  
  838.             ShowRequest(Window,LocaleString(MSG_PRINT_ERROR_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),Error,ErrorString);
  839.         }
  840.     }
  841. }
  842.  
  843.     /* PrintRegion(LONG Top,LONG Bottom):
  844.      *
  845.      *    Print the contents of a screen region.
  846.      */
  847.  
  848. VOID
  849. PrintRegion(LONG Top,LONG Bottom,BOOL FormFeed)
  850. {
  851.     BPTR     SomeFile;
  852.     LONG     i,j;
  853.     UBYTE    *Buffer;
  854.  
  855.     if(PrinterCapture)
  856.     {
  857.         LONG Len = strlen(LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_FOLLOWS_TXT));
  858.  
  859.         if(Write(PrinterCapture,LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_FOLLOWS_TXT),Len) < Len)
  860.         {
  861.             ShowRequest(Window,LocaleString(MSG_CONSOLE_ERROR_WRITING_TO_PRINTER_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT));
  862.  
  863.             return;
  864.         }
  865.  
  866.         SomeFile = PrinterCapture;
  867.     }
  868.     else
  869.     {
  870.         if(!(SomeFile = Open("PRT:",MODE_NEWFILE)))
  871.         {
  872.             ShowRequest(Window,LocaleString(MSG_TERMMAIN_FAILED_TO_OPEN_PRINTER_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT));
  873.  
  874.             return;
  875.         }
  876.     }
  877.  
  878.     for(i = Top ; i < Bottom ; i++)
  879.     {
  880.         Buffer = &Raster[i * RasterWidth];
  881.  
  882.         j = LastColumn;
  883.  
  884.         while(j >= 0 && Buffer[j] == ' ')
  885.             j--;
  886.  
  887.         if(j >= 0)
  888.         {
  889.             SetIoErr(0);
  890.  
  891.             if(Write(SomeFile,Buffer,j + 1) < j + 1)
  892.             {
  893.                 FormFeed = FALSE;
  894.  
  895.                 break;
  896.             }
  897.         }
  898.  
  899.         SetIoErr(0);
  900.  
  901.         if(Write(SomeFile,"\n",1) < 1)
  902.         {
  903.             FormFeed = FALSE;
  904.  
  905.             break;
  906.         }
  907.     }
  908.  
  909.     if(PrinterCapture)
  910.     {
  911.         LONG Len = strlen(LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_ENDING_TXT));
  912.  
  913.         Write(PrinterCapture,LocaleString(MSG_CONSOLE_SCREEN_PRINTOUT_ENDING_TXT),Len);
  914.     }
  915.     else
  916.     {
  917.         if(FormFeed)
  918.             Write(SomeFile,"\f",1);
  919.  
  920.         Close(SomeFile);
  921.     }
  922. }
  923.  
  924.     /* PrintScreenGfx():
  925.      *
  926.      *    Print the window contents as graphics.
  927.      */
  928.  
  929. BOOL
  930. PrintScreenGfx()
  931. {
  932.     LONG             Error;
  933.     struct MsgPort    *PrintPort;
  934.  
  935.         // Create the printer port
  936.  
  937.     if(PrintPort = CreateMsgPort())
  938.     {
  939.         struct IODRPReq *PrintRequest;
  940.  
  941.             // Create the rastport dump request
  942.  
  943.         if(PrintRequest = (struct IODRPReq *)CreateIORequest(PrintPort,sizeof(struct IODRPReq)))
  944.         {
  945.                 // Open the printer driver
  946.  
  947.             if(!OpenDevice("printer.device",0,PrintRequest,NULL))
  948.             {
  949.                 struct RastPort *RPort;
  950.  
  951.                     // Create a new rastport
  952.  
  953.                 if(RPort = (struct RastPort *)AllocVecPooled(sizeof(struct RastPort),MEMF_ANY))
  954.                 {
  955.                     struct BitMap    *BitMap;
  956.                     LONG             Width,Height;
  957.  
  958.                         // Initialize the rastport
  959.  
  960.                     InitRastPort(RPort);
  961.  
  962.                         // Keep these handy
  963.  
  964.                     Width    = Window -> Width        - (Window -> BorderLeft + Window -> BorderRight);
  965.                     Height    = Window -> Height        - (Window -> BorderTop + Window -> BorderBottom);
  966.  
  967.                     if(StatusWindow)
  968.                         Height -= StatusDisplayHeight;
  969.  
  970.                         // Allocate offscreen buffer to hold the window contents
  971.  
  972.                     if(BitMap = CreateBitMap(Width,Height,GetBitMapDepth(Window -> RPort -> BitMap),NULL,Window -> RPort -> BitMap))
  973.                     {
  974.                         struct EasyStruct     Easy;
  975.                         struct Window        *ReqWindow;
  976.  
  977.                             // Put the bitmap into the rastport
  978.  
  979.                         RPort -> BitMap = BitMap;
  980.  
  981.                             // Clear the bitmap
  982.  
  983.                         SetRast(RPort,0);
  984.  
  985.                             // Forbid any display changes
  986.  
  987.                         LockLayerRom(Window -> RPort -> Layer);
  988.  
  989.                             // Copy the window contents to the offscreen buffer
  990.  
  991.                         ClipBlit(Window -> RPort,Window -> BorderLeft,Window -> BorderTop,RPort,0,0,Width,Height,MINTERM_COPY);
  992.  
  993.                             // Permit display changes
  994.  
  995.                         UnlockLayerRom(Window -> RPort -> Layer);
  996.  
  997.                             // Wait for the bitmap to be transferred
  998.  
  999.                         WaitBlit();
  1000.  
  1001.                             // Set up the print request
  1002.  
  1003.                         PrintRequest -> io_Command        = PRD_DUMPRPORT;
  1004.                         PrintRequest -> io_RastPort        = RPort;
  1005.                         PrintRequest -> io_ColorMap        = Window -> WScreen -> ViewPort . ColorMap;
  1006.                         PrintRequest -> io_Modes        = GetVPModeID(&Window -> WScreen -> ViewPort);
  1007.                         PrintRequest -> io_SrcWidth        = Width;
  1008.                         PrintRequest -> io_SrcHeight    = Height;
  1009.  
  1010.                             // Set up the abort requester
  1011.  
  1012.                         Easy . es_StructSize    = sizeof(Easy);
  1013.                         Easy . es_Flags            = NULL;
  1014.                         Easy . es_Title            = LocaleString(MSG_TERMAUX_TERM_REQUEST_TXT);
  1015.                         Easy . es_GadgetFormat    = LocaleString(MSG_GLOBAL_ABORT_GAD);
  1016.                         Easy . es_TextFormat    = LocaleString(MSG_PRINTING_SCREEN_TXT);
  1017.  
  1018.                             // Create the abort requester
  1019.  
  1020.                         if(ReqWindow = BuildEasyRequest(Window,&Easy,NULL))
  1021.                         {
  1022.                             ULONG Signals;
  1023.  
  1024.                                 // Everything is fine so far
  1025.  
  1026.                             Error = 0;
  1027.  
  1028.                                 // Start printing
  1029.  
  1030.                             BeginIO(PrintRequest);
  1031.  
  1032.                                 // Run until everything is done
  1033.  
  1034.                             FOREVER
  1035.                             {
  1036.                                     // Wait for an event
  1037.  
  1038.                                 Signals = Wait(PORTMASK(ReqWindow -> UserPort) | PORTMASK(PrintPort));
  1039.  
  1040.                                     // Is the printer finished?
  1041.  
  1042.                                 if(Signals & PORTMASK(PrintPort))
  1043.                                 {
  1044.                                         // Wait for the request to return and check for error
  1045.  
  1046.                                     switch(WaitIO(PrintRequest))
  1047.                                     {
  1048.                                         case PDERR_NOTGRAPHICS:
  1049.  
  1050.                                             Error = ERR_NO_GFX_OUTPUT;
  1051.                                             break;
  1052.  
  1053.                                         case PDERR_BADDIMENSION:
  1054.  
  1055.                                             Error = ERR_BAD_DIMENSION;
  1056.                                             break;
  1057.  
  1058.                                         case IOERR_OPENFAIL:
  1059.  
  1060.                                             Error = ERR_NO_PRINTER;
  1061.                                             break;
  1062.  
  1063.                                         case PDERR_INTERNALMEMORY:
  1064.                                         case PDERR_BUFFERMEMORY:
  1065.  
  1066.                                             Error = ERR_NO_MEM;
  1067.                                             break;
  1068.                                     }
  1069.  
  1070.                                     break;
  1071.                                 }
  1072.  
  1073.                                     // Did the user press the abort button?
  1074.  
  1075.                                 if(Signals & PORTMASK(ReqWindow -> UserPort))
  1076.                                 {
  1077.                                     if(!SysReqHandler(ReqWindow,NULL,FALSE))
  1078.                                     {
  1079.                                         AbortIO(PrintRequest);
  1080.  
  1081.                                         WaitIO(PrintRequest);
  1082.  
  1083.                                         break;
  1084.                                     }
  1085.                                 }
  1086.                             }
  1087.  
  1088.                                 // Close the requester
  1089.  
  1090.                             FreeSysRequest(ReqWindow);
  1091.                         }
  1092.                         else
  1093.                             Error = ERR_NO_MEM;
  1094.  
  1095.                             // Free the memory allocated for the bitmap
  1096.  
  1097.                         DeleteBitMap(BitMap);
  1098.                     }
  1099.                     else
  1100.                         Error = ERR_NO_MEM;
  1101.  
  1102.                         // Free the rastport
  1103.  
  1104.                     FreeVecPooled(RPort);
  1105.                 }
  1106.                 else
  1107.                     Error = ERR_NO_MEM;
  1108.  
  1109.                     // Close the printer driver
  1110.  
  1111.                 CloseDevice(PrintRequest);
  1112.             }
  1113.             else
  1114.                 Error = ERR_NO_PRINTER;
  1115.  
  1116.                 // Free the rastport dump request
  1117.  
  1118.             DeleteIORequest(PrintRequest);
  1119.         }
  1120.         else
  1121.             Error = ERR_NO_MEM;
  1122.  
  1123.             // Free the printer orpt
  1124.  
  1125.         DeleteMsgPort(PrintPort);
  1126.     }
  1127.     else
  1128.         Error = ERR_NO_MEM;
  1129.  
  1130.         // Return the result
  1131.  
  1132.     if(Error)
  1133.     {
  1134.         SetIoErr(Error);
  1135.  
  1136.         return(FALSE);
  1137.     }
  1138.     else
  1139.         return(TRUE);
  1140. }
  1141.